import sys
import logging
import os
import traceback

# 设置日志
if sys.platform == 'darwin':
    log_file = os.path.expanduser('~/Library/Logs/ws_notifier.log')
else:
    log_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ws_notifier.log')

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler(log_file),
        logging.StreamHandler()
    ]
)

logger = logging.getLogger(__name__)

def main():
    try:
        # 测试模式
        if len(sys.argv) > 1 and sys.argv[1] == '--test':
            logger.info('Test mode - application starts successfully')
            return 0

        from ws_notifier.gui.tray import SystemTray
        from ws_notifier.config import Config
        
        logger.info("Starting application...")
        config = Config()
        tray = SystemTray(config)
        logger.info("Running system tray...")
        tray.run()
    except Exception as e:
        logger.error(f"Application failed to start: {e}")
        logger.error(traceback.format_exc())
        # 在 Windows 上保持窗口打开以查看错误
        if sys.platform == 'win32':
            input("Press Enter to exit...")
        return 1

if __name__ == '__main__':
    try:
        sys.exit(main())
    except Exception as e:
        logger.error(f"Unhandled exception: {e}")
        logger.error(traceback.format_exc())
        if sys.platform == 'win32':
            input("Press Enter to exit...")
        sys.exit(1) 